package com.lanxiao.cloud.modules.user.application;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanxiao.cloud.common.core.dto.PageQuery;
import com.lanxiao.cloud.common.core.dto.ResultModel;
import com.lanxiao.cloud.common.core.dto.UserBaseDto;
import com.lanxiao.cloud.common.core.error.LoginErrorType;
import com.lanxiao.cloud.common.core.exception.BusinessException;
import com.lanxiao.cloud.common.core.service.IOperatorService;
import com.lanxiao.cloud.common.web.service.ILog;
import com.lanxiao.cloud.modules.user.application.assembler.UserAssembler;
import com.lanxiao.cloud.modules.user.application.dto.UserDeptPageDto;
import com.lanxiao.cloud.modules.user.application.dto.UserDeptQuery;
import com.lanxiao.cloud.modules.user.application.dto.UserDto;
import com.lanxiao.cloud.modules.user.application.dto.UserPageDto;
import com.lanxiao.cloud.modules.user.application.dto.UserQuery;
import com.lanxiao.cloud.modules.user.application.query.UserLambdaQuery;
import com.lanxiao.cloud.modules.user.application.query.UserLambdaQueryWrapper;
import com.lanxiao.cloud.modules.user.domain.model.User;
import com.lanxiao.cloud.modules.user.domain.service.IUserService;
import com.lanxiao.cloud.user.api.dto.UserDubboDto;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Component;

/**
 * @author ：qiaoliang
 * @date ：2021-09-13
 */
@Component
public class UserQueryManager implements ILog<User, Long> {

  private final IUserService userService;
  private final UserAssembler userAssembler;
  private final IOperatorService operatorService;

  public UserQueryManager(
      IUserService userService,
      UserAssembler userAssembler,
      IOperatorService operatorService) {
    this.userService = userService;
    this.userAssembler = userAssembler;
    this.operatorService = operatorService;
  }

  /**
   *  条件分页查询用户列表
   *
   * @author ：qiaoliang
   * @param query : 查询条件
   * @param pageQuery : 分页条件
   * @return com.lanxiao.cloud.common.core.dto.ResultModel<com.lanxiao.cloud.modules.user.application.dto.UserPageDto>
   * @date 2021-09-14 10:00
   */
  public ResultModel<UserPageDto> query(UserQuery query, PageQuery pageQuery) {
    final LambdaQueryWrapper<User> queryWrapper = UserLambdaQueryWrapper.wrapper()
        .queryWrapper(UserLambdaQuery.toUserLambdaQuery(query))
        .geTime(pageQuery.getTime())
        .orderByDesc(User::getCreateTime)
        .query();
    return userAssembler.toUserPageDto(userService.page(
        new Page<>(pageQuery.getPage(), pageQuery.getSize()), queryWrapper));
  }

  /**
   *  条件分页查询部门用户列表
   *
   * @author ：qiaoliang
   * @param query : 查询条件
   * @param pageQuery : 分页条件
   * @return com.lanxiao.cloud.common.core.dto.ResultModel<com.lanxiao.cloud.modules.user.application.dto.UserPageDto>
   * @date 2021-09-14 10:00
   */
  public ResultModel<UserDeptPageDto> query(UserDeptQuery query, PageQuery pageQuery) {
    final LambdaQueryWrapper<User> queryWrapper = UserLambdaQueryWrapper.wrapper()
        .queryWrapper(UserLambdaQuery.toUserLambdaQuery(query))
        .geTime(pageQuery.getTime())
        .orderByDesc(User::getCreateTime)
        .query();
    return userAssembler.toUserDeptPageDto(userService.page(
        new Page<>(pageQuery.getPage(), pageQuery.getSize()), queryWrapper));
  }

  /**
   * 昵称后置模糊查询
   *
   * @author ：qiaoliang
   * @param name : 昵称
   * @return java.util.List<com.lanxiao.cloud.modules.user.application.dto.UserDto>
   * @date 2021-09-14 10:03
   */
  public List<UserDto> list(String name) {
    final LambdaQueryWrapper<User> query = UserLambdaQueryWrapper.wrapper()
        .queryWrapper(UserLambdaQuery.toUserName(name))
        .orderByDesc(User::getCreateTime).query();
    return userAssembler.toUserDto(userService.list(query));
  }

  /**
   *  获取当前登录的用户信息
   *
   * @author ：qiaoliang

   * @return com.lanxiao.cloud.modules.user.application.dto.UserDto
   * @date 2021-09-14 13:54
   */
  public UserDto info() {
    return userAssembler.toUserDto(userService.getById(operatorService.operator()));
  }

  /**
   *  校验是否可以登陆
   *
   * @author ：qiaoliang
   * @param userId : 用户ID
   * @date 2021-09-13 17:37
   */
  public UserDubboDto checkLogin(Long userId) throws BusinessException {
    final User user = userService.getById(userId);
    BusinessException.isNull(user, LoginErrorType.ACCOUNT_IS_NULL_ERROR);
    user.checkLogin();
    return userAssembler.toUserDubboDto(user);
  }

  /**
   *  查询用户信息，返回map格式<用户ID,用户信息模型>
   *
   * @author ：qiaoliang
   * @param ids : 用户ID集合
   * @return java.util.Map<java.lang.Long, com.lanxiao.cloud.common.core.dto.UserBaseDto < java.lang.Long>>
   * @date 2021-09-13 17:38
   */
  public Map<Long, UserBaseDto<Long>> findByIds(List<Long> ids) {
    final Map<Long, User> userMap = userService.findByIds(ids);
    return userAssembler.toUserBaseDto(userMap);
  }

  @Override
  public User selectById(Long id) {
    return userService.getById(id);
  }

}
